package com.yuchen.ishop.service.file;


import com.yuchen.ishop.vo.FileResponse;
import com.yuchen.utils.config.Config;
import com.yuchen.utils.config.ConfigUtils;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
import org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataOutput;
import org.springframework.stereotype.Service;

import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.*;

/**
 * @author Jack Q
 *         文件服务客户端
 */
@Service
public class FileRestClient {

    private final static Logger LOG = Logger.getLogger(FileRestClient.class);

    public static void main(String[] args) throws FileNotFoundException {

        String fileType = "txt";
        String folder = "txt";
        InputStream io = new FileInputStream(new File("d:\\aaa.txt"));


            FileRequest fileRequest = new FileRequest(fileType, folder, io);
            FileRestClient fileRestClient = new FileRestClient();
            fileRestClient.sendFile(fileRequest);

        //InputStream io = null;


//        String deleteUrl ="http://localhost:8889/services/fileService/delete";
//        String fileUrl = "image/201503/qwe.txt";
//        FileRestClient.deleteFile(deleteUrl, fileUrl);
    }

    /**
     * @param fileRequest
     * @return
     */
    public FileResponse sendFile(FileRequest fileRequest) {
        fileRequest.validations();
        ResteasyClient client = null;
        Response r = null;
        FileResponse json = null;
        try {
            String url = ConfigUtils.config.get("file_upload");
            client = new ResteasyClientBuilder().build();
            ResteasyWebTarget target = client.target(url);
            MultipartFormDataOutput mdo = new MultipartFormDataOutput();
            mdo.addFormData("fileType", new ByteArrayInputStream(fileRequest.getFileType().getBytes()), MediaType.TEXT_PLAIN_TYPE);
            mdo.addFormData("folder", new ByteArrayInputStream(fileRequest.getFolder().getBytes()), MediaType.TEXT_PLAIN_TYPE);
            mdo.addFormData("attachment", fileRequest.getIo(), MediaType.APPLICATION_OCTET_STREAM_TYPE);
            GenericEntity<MultipartFormDataOutput> entity = new GenericEntity<MultipartFormDataOutput>(mdo) {
            };
            r = target.request().post(Entity.entity(entity, MediaType.MULTIPART_FORM_DATA_TYPE));
            json = r.readEntity(FileResponse.class);
            LOG.info(json);
        } finally {
            if (r != null) r.close();
            if (client != null) client.close();
        }
        return json;
    }

    public FileResponse deleteFile(String fileUrl) {
        ResteasyClient client = null;
        Response r = null;
        FileResponse json = null;
        try {
            String url = ConfigUtils.config.get("file_delete");
            StringBuilder urlBuilder = new StringBuilder();
            urlBuilder.append(url).append("?fileUrl=").append(fileUrl);
            client = new ResteasyClientBuilder().build();
            ResteasyWebTarget target = client.target(urlBuilder.toString());
            r = target.request().get();
            json = r.readEntity(FileResponse.class);
            LOG.info(json);
        } finally {
            if (r != null) r.close();
            if (client != null) client.close();
        }
        return json;
    }

}
